/**
 * Carnegie Mellon University
 * School of Computer Science
 * MSIT eBusiness Technology
 * 
 * Team 8 - Infinity (2011 Spring)
 * infinity-team8@googlegroups.com
 * 
 * Professor: Jeffrey L. Eppinger
 * Program Faculty: Sujata Telang
 */

package model;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Arrays;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanFactoryException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.DuplicateKeyException;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import databean.TransactionBean;


public class TransactionDAO {
	private BeanFactory<TransactionBean> factory;
	
	public TransactionDAO() throws DAOException {
		try {
			// Get a BeanTable so we can create the "user" table
	        BeanTable<TransactionBean> transactionTable = BeanTable.getInstance(TransactionBean.class,"transaction");
	        
	        if (!transactionTable.exists()) transactionTable.create("transactionID");
	        
	        // Long running web apps need to clean up idle database connections.
	        // So we can tell each BeanTable to clean them up.  (You would only notice
	        // a problem after leaving your web app running for several hours.)
	        transactionTable.setIdleConnectionCleanup(true);
	
	        // Get a BeanFactory which the actions will use to read and write rows of the "user" table
	        factory = transactionTable.getFactory();
	        
	        
		} catch (BeanFactoryException e) {
			throw new DAOException(e);
		}
	}
	
	public void create(TransactionBean transaction) throws DAOException {
		try {
    	
        	Transaction.begin();
        	
        	TransactionBean dbTransaction = factory.create();
			factory.copyInto(transaction,dbTransaction);
			
			Transaction.commit();
			
		} catch (DuplicateKeyException e) {
			throw new DAOException(e);
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}
	
	public void modify(TransactionBean transaction) throws DAOException {
		try {
    	
        	Transaction.begin();
        	
        	TransactionBean dbTransaction = factory.lookup(transaction.getTransactionID());
        	dbTransaction.setExecuteDate(transaction.getExecuteDate());
        	dbTransaction.setShares(transaction.getShares());
        	dbTransaction.setSharePrice(transaction.getSharePrice());
        	dbTransaction.setAmount(transaction.getAmount());

			Transaction.commit();
			
		} catch (DuplicateKeyException e) {
			throw new DAOException(e);
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}
	
	/**
	 * lookup fund by transaction id
	 * @param name
	 * @return
	 * @throws DAOException
	 */
	public TransactionBean lookup(long transactionID) throws DAOException {
		try {
			return factory.lookup(transactionID);
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
	
	/**
	 * lookup fund by customer id
	 * @param name
	 * @return
	 * @throws DAOException
	 */
	public TransactionBean[] lookupByCustomerID(long customerID) throws DAOException {
		try {
			
			if (factory.match(MatchArg.equals("customerID", customerID)) == null ||
            		factory.match(MatchArg.equals("customerID", customerID)).length == 0) {
				return null;
			} else {
				return factory.match(MatchArg.equals("customerID", customerID));
			}
			
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
	
	
	/**
	 * look up shares by transaction id
	 * @param userName
	 * @return
	 * @throws DAOException
	 */
	public String getSharesFormatted(long transactionID) throws DAOException {
		TransactionBean bean = lookup(transactionID);
		long shares = bean.getShares();
		
		NumberFormat formatter = new DecimalFormat("##0.000");
		String s = formatter.format((double)shares/1000);
		
		return s;
	}
	
	/**
	 * look up share price by transaction id
	 * @param userName
	 * @return
	 * @throws DAOException
	 */
	public String getSharePriceFormatted(long transactionID) throws DAOException {
		TransactionBean bean = lookup(transactionID);
		long sharePrice = bean.getSharePrice();
		
		NumberFormat formatter = new DecimalFormat("##0.00");
		String s = formatter.format((double)sharePrice/100);
		
		return s;
	}
	
	/**
	 * look up amount by transaction id
	 * @param userName
	 * @return
	 * @throws DAOException
	 */
	public String getAmountFormatted(long transactionID) throws DAOException {
		TransactionBean bean = lookup(transactionID);
		long amount = bean.getAmount();
		
		NumberFormat formatter = new DecimalFormat("##0.00");
		String s = formatter.format((double)amount/100);
		
		return s;
	}
	
	
	protected BeanFactory<TransactionBean> getFactory() { return factory; }
	
	public TransactionBean[] getTransactions() throws DAOException {
		try {
			TransactionBean[] transactions = factory.match();
			Arrays.sort(transactions);
			return transactions;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
	
	public TransactionBean[] getPendingTransactions() throws DAOException {
		try {
			TransactionBean[] transactions = factory.match(MatchArg.equals("executeDate", null));
			Arrays.sort(transactions);
			return transactions;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
	
	
}
